package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.utils.Constants;

public class Cellar extends Action {

	public Cellar() {
		this.cost = 2;
	}

	@Override
	public Card clone() {
		return new Cellar();
	}

	@Override
	public String toString() {
		return Constants.CELLAR;
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("+1 action\n");
		sb.append("Discard any number of cards.\n");
		sb.append("+1 card for each discarded.\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		playedCard(player);
		Constants.printer.print("Type cards to discard. Type \"done\" when finished");
		Constants.printer.print(player.printHand());

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String input = null;
		boolean done = false;
		int cardsToDraw = 0;
		while (!done && !player.getHand().isEmpty())
		{
			input = in.readLine().trim();
			if (input.equalsIgnoreCase("done"))
			{
				done = true;
			}
			else if (player.getHand().contains(input))
			{
				Card card = player.getHand().getCard(input);
				card = player.drawFromDeck(card.toString(), Constants.HAND);
				player.addToDeck(card, Constants.DISCARD_DECK);
				cardsToDraw++;
			}
			else
			{
				Constants.printer.print("Card does not exist in hand.");
			}
		}

		for (int i = 0; i < cardsToDraw; i++)
		{
			player.drawCard();
		}
		Constants.printer.print("New hand:");
		Constants.printer.print(player.printHand());
	}
}
